﻿using CampusTrade.Pojo;
using CampusTrade.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CampusTrade.Controller
{
    [Route("api/[controller]")]
    [ApiController]
    public class TransactionController : ControllerBase
    {
        private readonly TransactionService transactionService;
        private readonly GoodService goodService;

        public TransactionController(TransactionService transactionService, GoodService goodService)
        {
            this.transactionService = transactionService;
            this.goodService = goodService;
        }
        /**
         * 发起交易
         * 更改供需信息状态为3 添加到交易表
         */
        [HttpPost("addTransaction")]
        public long addTransaction(Transaction transaction)
        {
            transaction.CreateTime = (long)(DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds;
            goodService.changeStatus(transaction.GoodId, 3);
            return transactionService.addTransaction(transaction);
        }
        /**
         * 接收交易请求
         * 更改供需信息状态为4
         */
        [HttpPut("acceptTransaction")]
        public int acceptTransaction(long goodId)
        {
            return goodService.changeStatus(goodId, 4);
        }
        /**
         * 拒绝交易请求
         * 更改供需信息状态为2
         */
        [HttpDelete("refuseTransaction")]
        public int refuseTransaction(long id, long goodId)
        {
            goodService.changeStatus(goodId, 2);
            return transactionService.delTransaction(id);
        }
        /**
         * 获取交易的详细信息
         * 根据交易id获取交易者的uid进而获得交易者信息，交易时间地址等
         */
        [HttpGet("getMyTransactionDetail/{id}")]
        public Transaction getMyTransactionDetail(long id)
        {
            return transactionService.getTransactionDetail(id);
        }
        /**
         * 获取关于自己的出售列表
         * 交易表里有的good的type=='收'且trade_user_id=uid
         */
        [HttpGet("getMySellList/{uid}")]
        public List<Transaction> getMySellList(long uid)
        {
            return transactionService.getSellList(uid);
        }
        /**
         * 获取关于自己的购买列表
         * 交易表里有的good的type=='出'且trade_user_id=uid
         */
        [HttpGet("getMyBuyList/{uid}")]
        public List<Transaction> getMyBuyList(long uid)
        {
            return transactionService.getBuyList(uid);
        }
    }
}
